package com.example.superoa.model.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.KqAttendanceDao;
import com.example.superoa.model.dao.KqBanciDao;
import com.example.superoa.model.dao.KqOverworkStatiDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.mappers.BanCiMapper;
import com.example.superoa.model.mappers.KqAttendanceMapper;
import com.example.superoa.model.mappers.LeaveMapper;
import com.example.superoa.model.mappers.OvertimeMapper;
import com.example.superoa.model.mappers.OvertimestatiMapper;
import com.example.superoa.model.mappers.RecessMapper;
import com.example.superoa.model.pojos.KqAttendance;
import com.example.superoa.model.pojos.KqBanci;
import com.example.superoa.model.pojos.KqOverworkStati;
import com.example.superoa.model.pojos.Staff;

@Service
@Transactional(rollbackFor=Exception.class)
public class DakaService {
	@Autowired
	private KqAttendanceDao dao;
	
	@Autowired
	private BanCiMapper b_map;
	
	@Autowired
	private KqBanciDao bc_dao;
	
	@Autowired
	private OvertimeService o_ser;
	
	@Autowired
	private StaffDao sta;
	
	@Autowired
	private LeaveMapper l_map;
	
	@Autowired
	private KqAttendanceMapper map;
	
	@Autowired
	private OvertimestatiMapper os_map;
	
	@Autowired
	private RecessMapper r_map;
	
	@Autowired
	private LeaveService l_biz;
	
	@Autowired
	private KqOverworkStatiDao os_dao;
	
	private Shijian date=new Shijian();
	
	public JSONObject panduan(int staff_id) {
		SimpleDateFormat sdf=new SimpleDateFormat("YYYYMMdd");
		JSONObject obj=null;
		String datetime=sdf.format(new Date(date.getNetworkTime()));
		JSONArray json=map.selectAllId(staff_id);
		for (Object object : json) {
			JSONObject jsono=(JSONObject) object;
				String[] str=jsono.get("a_date").toString().split("-");
				String ss="";
				for (String string : str) {
					ss+=string;
				}
					if(Integer.parseInt(ss)==Integer.parseInt(datetime)) {
						obj=jsono;
						return obj;
					}
		}
		return null;
	} 
	public int Zhunshi(KqBanci banci) {
		Time b_tui=banci.getBTui();
		Time b_dao=banci.getBDao();
		Time time=new Time(date.getNetworkTime());
		if(b_dao.compareTo(time)>=0) {
			return 1;
		}else if(b_tui.compareTo(time)<=0){
			return 2;
		}else {
			return 3;
		}
	}
	public String Tui(KqAttendance pojos,Time time) {
		KqBanci banci=pojos.getKqBanci();	
		JSONObject leave=l_map.selectBqLeave(pojos.getStaff().getStaffId(), pojos.getADate());
		Time b_tui=banci.getBTui();
		if(leave!=null&&leave.getInteger("L_TYPE")!=1) {
			if(leave.getInteger("L_TYPE")==3) {
				b_tui=new Time(date.getDaiTime(banci.getBDao(), 4).getTime());
			}
		}
		BigDecimal shichu;
		int i=date.getPanduanShi1(time, b_tui);
		pojos.setATui(time);
		JSONObject jia=o_ser.selectbetween(pojos.getStaff().getStaffId(), pojos.getADate());
		String str;
		if(jia==null||jia.getInteger("OX_TYPE")==0) {
			if(i==1 || i==0) {
				str="打卡成功, 辛苦的好员工";
				if(pojos.getAKuang().compareTo(new BigDecimal(1))==0) {
					pojos.setAKuang(new BigDecimal(0.5));
				}
			}else {
				shichu=date.getyuansuan(pojos.getADao(), time);
				str="打卡成功，早退的坏员工";
				//当早退了，而且有请假记录时
				if(leave!=null&&leave.getInteger("L_TYPE")!=1) {
					//早退在一个小时之内
					if(date.getyuansuan(b_tui,time).compareTo(new BigDecimal(1))<=0) {
						//迟到一个小时以上时
						if(pojos.getAKuang().compareTo(new BigDecimal(0.5))==0) {
							pojos.setAZaotui(0);
						//迟到一个小时以内时
						}else {
							pojos.setAZaotui(1);
							pojos.setAKuang(new BigDecimal(0));
						}
					//早退在一个小时之上时
					}else {
						pojos.setAZaotui(0);
						pojos.setAKuang(new BigDecimal(1));	
					}
				//无请假记录时
				}else {
					//早退在一个小时之内
					if(date.getyuansuan(time,b_tui).compareTo(new BigDecimal(1))<=0) {
						//迟到一个小时以上时
						if(pojos.getAKuang().compareTo(new BigDecimal(1))==0) {
							pojos.setAZaotui(1);
							if(shichu.compareTo(new BigDecimal(4))>=0) {
								pojos.setAKuang(new BigDecimal(0.5));
							}else {
								pojos.setAZaotui(0);
								pojos.setAKuang(new BigDecimal(1));
							}
						//迟到一个小时以内时
						}else {
							pojos.setAZaotui(1);
							pojos.setAKuang(new BigDecimal(0));
						}
					//早退在一个小时之上时
					}else {
						//迟到一个小时以上
						if(pojos.getAKuang().compareTo(new BigDecimal(1))==0) {
							pojos.setAZaotui(0);
							pojos.setAKuang(new BigDecimal(1));
						//迟到一个小时以内时
						}else {
							pojos.setAZaotui(0);
							if(shichu.compareTo(new BigDecimal(4))>=0) {
								pojos.setAKuang(new BigDecimal(0.5));
							}else {
								pojos.setAChidao(0);
								pojos.setAKuang(new BigDecimal(1));
							}
							
						}	
					}
				}
				//旷工一天
				if(pojos.getAKuang().compareTo(new BigDecimal(1))==0) {
					pojos.setAShichu(new BigDecimal(0));
				//旷工半天
				}else if(pojos.getAKuang().compareTo(new BigDecimal(0.5))==0) {
					pojos.setAShichu(new BigDecimal(0.5));
				//无旷工
				}else {
					pojos.setAShichu(new BigDecimal(1));
				}
			}
		}else{
			pojos.setAShichu(date.getShichu(pojos.getADao(), time, banci));
			str="打卡成功，加班辛苦了";
		}
		dao.save(pojos);
		return str;
	}
	public String Dao(int staff_id,Time time,Date dang,KqAttendance pojos) {
		JSONObject banci=b_map.selectqiyong();
		JSONObject leave=l_map.selectBqLeave(staff_id, dang);
		Integer type=null;
		int i;
		try {
			type=o_ser.selectbetween(staff_id, dang).getInteger("OX_TYPE");
		} catch (Exception e) {
			// TODO: handle exception
		}
		Time b_dao = new Time(banci.getDate("b_dao").getTime());
		if(leave!=null&&leave.getInteger("L_TYPE")!=1) {
			if(leave.getInteger("L_TYPE")==2) {
				b_dao=new Time(date.getDaiTime2(banci.getDate("b_tui"), 4).getTime());
				i=date.getQiandao(time,b_dao);
			}
		}
		String str;
		i=date.getQiandao(time,b_dao);
		System.out.println("到---"+b_dao+"---"+i);
		Staff  staff=sta.findById(staff_id).get();
		pojos.setADate(dang);
		pojos.setADao(time);
		pojos.setStaff(staff);		
		if(type==null || type==0) {
			if(i==-1 || i==0) {
				 str="打卡成功, 准时的好员工";
				 pojos.setAChidao(0);
				 pojos.setAKuang(new BigDecimal(0));
			}else {
				str="打卡成功，迟到的坏员工";
				System.out.println(leave);
				//当迟到时，找到请假记录
				if(leave!=null&&leave.getInteger("L_TYPE")!=1) {
					//迟到在一小时以上时
					if(date.getyuansuan(time, b_dao).compareTo(new BigDecimal(1))>=0) {
						pojos.setAChidao(0);
						pojos.setAKuang(new BigDecimal(0.5));
					}else {
						pojos.setAChidao(1);
						pojos.setAKuang(new BigDecimal(0));
					}
				//无请假
				}else {
					//迟到在一小时以上
					if(date.getyuansuan(time, b_dao).compareTo(new BigDecimal(1))>=0) {
						pojos.setAChidao(0);
						pojos.setAKuang(new BigDecimal(1));
					}else {
						pojos.setAChidao(1);
						pojos.setAKuang(new BigDecimal(0));
					}
				}
				
			}
		}else {
			str="打卡成功，勤奋的好员工";
		}
		pojos.setKqBanci(bc_dao.findById(banci.getInteger("b_id")).get());
		pojos.setAZaotui(0);
		dao.save(pojos);
		return str;
	}
	public JSONObject selectBanci() {
		return b_map.selectqiyong();
	}
	public KqAttendance selectid(int id) {
		return dao.findById(id).get();
	}
	public boolean selectPanduan(Integer staff_id,Date newdate) {
		JSONObject qingjia=null;
		boolean opin=date.panduanSun(newdate);
		if(l_biz.selectBqLeave(staff_id, newdate)!=null) {
			qingjia=l_biz.selectBqLeave(staff_id, newdate);
			if(qingjia!=null&&qingjia.getInteger("L_TYPE")!=1) {
				return false;
			}
		}
		if(r_map.selectjiaoOne(newdate)!=null||r_map.selectjiaOne(newdate)!=null||opin) {
			if(o_ser.selectbetween(staff_id, newdate)!=null) {
				return false;
			}
		}
		return true;
	}
	public void upaddOS(Staff staff,JSONObject ob,Date time) {
		KqOverworkStati overstati=null;
		List<Integer> list = null;
		try {
			list=date.getyearmonth(time);
		} catch (Exception e) {
			// TODO: handle exception
		}
		JSONObject obj=os_map.selectOverstatiId(list.get(0), list.get(1), staff.getStaffId());
		int i=0;
		if(obj!=null) {
			overstati=os_dao.findById(obj.getInteger("OS_ID")).get();
		}else {
			overstati=new KqOverworkStati();
			overstati.setStaff(staff);
			if(os_map.selectOverstatilaft()==null) {
				i=1;
			}else {
				i=os_map.selectOverstatilaft()+1;
			}
			overstati.setOsId(i);
		}
		overstati.setOsPing(ob.getBigDecimal("pingjia"));
		overstati.setOsXiu(ob.getBigDecimal("xiujia"));
		overstati.setOsJie(ob.getBigDecimal("jiejia"));
		overstati.setOsDate(new Date(date.getNetworkTime()));
		os_dao.save(overstati);
	}
}
